<p class="help-wx">
	<i class="fa fa-info-circle fa-fw"></i>&{'weixin.welcome.notice.1'}
</p>

<div class="panel panel-default" ng-controller="welcomeCtrl">
	<div class="panel-heading">&{'weixin.welcome.caption'}</div>
	<div class="panel-body">
		<form id="weixin-welcome-form" action="#" method="post" class="form-horizontal form-wx">
			<div class="form-group">
				<label class="col-sm-2 control-label">&{'resource.type'}</label>
				<div class="col-sm-10 radio">
					#{list items: types, as: 't'}
					<label><input name="type" value="${t.key}" type="radio" ng-model="welcome.type" required>${t.value}</label>
					#{/list}
				</div>
			</div>
			<div class="form-group fade-animate" ng-if="welcome.type==='01'">
				<label class="col-sm-2 control-label">&{'resource.type.text'}</label>
				<div class="col-sm-6">
					<textarea name="value" rows="5" class="form-control" ng-model="welcome.value" required></textarea>
				</div>
			</div>
			<div class="form-group fade-animate" ng-if="welcome.type==='02'">
				<label for="value" class="col-sm-2 control-label">&{'resource.type.single'}</label>
				<div class="col-sm-10">
					<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-popover" ng-click="modal('@{Resources.article_popover('02', 'setArticle')}')">&{'resource.button.single'}</button>
					<input name="value" type="hidden" value="{{welcome.value}}">
					<div class="fade-animate single" ng-if="article" article="article" autoheight></div>
				</div>
			</div>
			<div class="form-group fade-animate" ng-if="welcome.type==='03'">
				<label for="value" class="col-sm-2 control-label">&{'resource.type.multi'}</label>
				<div class="col-sm-10">
					<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-popover" ng-click="modal('@{Resources.article_popover('03', 'setArticle')}')">&{'resource.button.multi'}</button>
					<input name="value" type="hidden" value="{{welcome.value}}">
					<div class="fade-animate single" ng-if="article_multi" article-multi="article_multi"></div>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-offset-2 col-sm-10">
					<button id="save-btn" type="button" class="btn btn-success" ng-click="save()" ng-disabled="btnDisabled()">
						<span ng-if="btn(['wait'])">&{'app.common.button.save'}</span>
						<span ng-if="btn(['doing'])"><i class="fa fa-spinner fa-spin"></i> &{'app.common.button.doing'}</span>
					</button>
				</div>
			</div>
		</form>
	</div>
	
	<!-- pic lib modal -->
	#{include 'Admin/modal.html' /}
	<!-- /pic lib modal -->
</div>

<script type="text/javascript">
'use strict'
var welcomeCtrl = ['$scope', '$http', function($scope, $http) {
	// 定义方法
	angular.extend($scope, {
		init: function() {
			// 监控类型变化
			$scope.$watch('welcome.type', function(nvalue, ovalue) {
				$scope.type[ovalue] = $scope.welcome.value;
				$scope.welcome.value = $scope.type[nvalue];
			});
			
			// 监控内容变化
			$scope.$watch('welcome.value', function(value) {
				if (angular.isUndefined(value)) return;
				if ($scope.welcome.type === '02') {
					$http({
						method: 'get',
						url: '@{Resources.article_get}?type=02&id=' + value,
						headers: {'Content-Type': 'application/x-www-form-urlencoded'}
					}).success(function(data) {
						$scope.article = data;
					});
				} else if ($scope.welcome.type === '03') {
					$http({
						method: 'get',
						url: '@{Resources.article_get}?type=03&id=' + value,
						headers: {'Content-Type': 'application/x-www-form-urlencoded'}
					}).success(function(data) {
						$scope.article_multi = data;
					});
				}
			});
			
			// 初始化数据
			$scope.btn('wait');
			$scope.welcome = {type: '${welcome.type}', value: '${welcome.value}'};
			$scope.type = [];
		}, btnDisabled: function() {
			return $scope.btn(['doing']);
		}, save: function() {
			$scope.btn('doing');
			$http({
				method: 'post',
				url: '@{Weixins.welcome_save}',
				data: angular.element('#weixin-welcome-form').serialize(),
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			}).success(function(data) {
				console.log(data);
				$scope.btn('wait');
			});
		}, modal: function(url) {
			$scope.modalUrl = url;
		}, setArticle: function(selected) {
			$scope.welcome.value = selected.id;
		}
	});
	
	// 初始化
	$scope.init();
}];
</script>
